package com.qq.BFMRSE.requestModule;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Queue;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;

import com.qq.BFMRSE.entity.indexTree;
import com.qq.BFMRSE.entity.indexTreeNode;
import com.qq.BFMRSE.extractWord.wordTFAndIDFInfo;
import com.qq.BFMRSE.index.buildIndex;
import com.qq.BFMRSE.index.indexTreeUpLoad;
import com.qq.BFMRSE.indexTree.buildIndexTreeHelper;
import com.qq.BFMRSE.serialize.objectSerializeUtil;


public class upLoadModule {
//	private buildIndex buIndex;
//	public buildIndex getBuIndex(){
//		return buIndex;
//	}
//	public void setBuIndex(buildIndex buIndex){
//		this.buIndex=buIndex;
//	}
//	public upLoadModule(buildIndex buIndex){
//		this.buIndex=buIndex;
//	}
	/**
	 * upload encrypted index information
	 * @param url
	 * @param content
	 * @param userName
	 * @return
	 */
	public static int upLoadEnIndexInfo(String url,Object content,String userName)
	{
		int stateCode=0;
		String encryptedIndexInfo=JSONObject.fromObject(content).toString();
		HttpClient httpClient=new HttpClient();
		PostMethod post=new PostMethod(url);
		post.addRequestHeader("userName", userName);
		post.setRequestBody(encryptedIndexInfo);
		try {
			httpClient.executeMethod(post);
			stateCode=post.getStatusCode();
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return stateCode;
		
	}
	public static int upLoadAllFileInfo(String url,Object root,String userName)
	{
		int stateCode=0;
		List<String>fileNameList=wordTFAndIDFInfo.getAllFileName();
		String indexTreeInfo=indexTreeUpLoad.getIndexTreeInfo((indexTreeNode)root);
		String levelBitInfo=indexTreeUpLoad.getIndexTreeBfs(fileNameList);
		//String encryptedIndexInfo=buIndex.getEncryptedIndexJsonInfo(fileNameList);
		HttpClient httpClient=new HttpClient();
		PostMethod postMethod=new PostMethod(url);
		postMethod.addParameter("indexTreeInfo", indexTreeInfo);
		postMethod.addParameter("levelBitInfo",levelBitInfo);
//		postMethod.addParameter("encryptedIndexInfo", encryptedIndexInfo);
		postMethod.addParameter("userName",userName);
		try {
			httpClient.executeMethod(postMethod);
			stateCode=postMethod.getStatusCode();
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return stateCode;
	}

}
